package com.sheep.demo.quartz.scheduler;

import com.sheep.demo.quartz.dto.RemoteJobParam;
import com.sheep.demo.quartz.service.QuartzLogService;
import com.sheep.demo.quartz.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

@Slf4j
public class RemoteJob extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext context) {
        RemoteJobParam param = (RemoteJobParam) context.getMergedJobDataMap().get("param");
        QuartzLogService quartzLogService = (QuartzLogService) SpringContextUtil.getBean("quartzLogService");
        // 定时器日志记录
//        QuartzLog quartzLog = new QuartzLog();
//        quartzLog.setJobId(quartzJob.getId());
//        quartzLog.setBeanName(quartzJob.getBeanName());
//        quartzLog.setParams(quartzJob.getParams());
//        quartzLog.setCreateTime(LocalDateTime.now());
        long beginTime = System.currentTimeMillis();
        try {
            // 加载并执行
            //TODO 远程执行
            long executeTime = System.currentTimeMillis() - beginTime;
//            quartzLog.setTimes((int) executeTime);
//            quartzLog.setState(LogState.LOG_SUS.getStatus());
        } catch (Exception e) {
            // 异常信息
            long executeTime = System.currentTimeMillis() - beginTime;
//            quartzLog.setTimes((int) executeTime);
//            quartzLog.setState(LogState.LOG_FAIL.getStatus());
//            quartzLog.setError(e.getMessage());
        } finally {
            // 保存执行日志
//            quartzLogService.insert(quartzLog);
        }
    }
}